import csv
import datetime
import os
import sys
import time

from sqlalchemy.orm import sessionmaker


class Base(object):

    def _fixup(self): 
        '''(over-ride me): _fixup is called after creating an object via dictionary '''

    @classmethod
    def get_filename(cls, ext='txt'):
        return '%s.%s' % (cls.__tablename__, ext)

    @classmethod
    def get_filepath(cls, directory='.', default='no file found'):
        ret_val = default 
        for x in ('txt', 'csv', 'doc'):
            file = cls.get_filename(x)
            file_path = os.path.join(directory, file)
            if os.path.exists(file_path):
                ret_val = file_path
                break

        return ret_val

    @classmethod
    def from_dict(cls, attrs):
        clean_dict = cls.make_record(attrs)
        c = cls(**clean_dict)
        c._fixup()
        return c

    @classmethod
    def make_record(cls, row):
        # clean dict
        for k, v in row.items():
            if isinstance(v, basestring):
                v = v.strip()
            if (k not in cls.__table__.c):
                del row[k]
            elif not v:
                row[k] = None
            elif k.endswith('date'):
                row[k] = datetime.datetime.strptime(v, '%Y%m%d').date()

        return row

    @classmethod
    def set_schema(cls, schema):
        cls.__table__.schema = schema

    @classmethod
    def load(cls, engine, directory='.', file=None):
        records = []
        if(file is None):
            file_path = cls.get_filepath(directory)
        else:
            file_path = os.path.join(directory, file)

        if os.path.exists(file_path):
            file = open(file_path, 'r')
            reader = csv.DictReader(file)
            table = cls.__table__
            engine.execute(table.delete())
            Session = sessionmaker(bind=engine)
            session = Session()
            i = 0
            for row in reader:
                #records.append(cls.make_record(row))
                records.append(cls.from_dict(row))
                i += 1
                # commit every 10,000 records to the database to manage memory usage
                if i >= 10000:
                    #engine.execute(table.insert(), records)
                    session.add_all(records)
                    session.flush()
                    session.commit()

                    records = []
                    i = 0
            if len(records) > 0:
                session.add_all(records)
                session.flush()
                session.commit()
                session.flush()
                session.close()
#                engine.execute(table.insert(), records)
            file.close()
